package com.css.bjdt_core.reportPrepare.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Condition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.report.service.ReportTemplateManageService;
import com.css.bjdt_core.reportPrepare.dao.RptLssuedAreasInfoDao;
import com.css.bjdt_core.reportPrepare.entity.RptLssuedAreasInfo;
import com.css.bjdt_core.reportPrepare.entity.RptLssuedAreasTemplateRel;
import com.css.bjdt_core.reportPrepare.entity.ViewRptGroupTemplate;

@Service
public class RptLssuedAreasInfoService {

	@Autowired
	private RptLssuedAreasInfoDao rptLssuedAreasInfoDao;

	@Autowired
	private RptDealWithService rptDealWithService;

	@Autowired
	private ReportTemplateManageService reportTemplateManageService;

	public void saveARptLssuedAreasInfo(RptLssuedAreasInfo rptLssuedAreasInfo) {
		rptLssuedAreasInfoDao.saveARptLssuedAreasInfo(rptLssuedAreasInfo);
	}

	public void updateARptLssuedAreasInfo(String areaId, String areaName) {
		rptLssuedAreasInfoDao.updateARptLssuedAreasInfo(areaName, areaId);
	}

	public void deleteARptLssuedAreasInfo(String areaId) {
		rptLssuedAreasInfoDao.deleteARptLssuedAreasInfoById(areaId);
	}

	public Map queryPage(Condition cnd, int pageNum, int pageSize) {
		return rptLssuedAreasInfoDao.queryPage(cnd, pageNum, pageSize);
	}

	public RptLssuedAreasInfo getARptLssuedAreasInfo(String areaId) {
		return rptLssuedAreasInfoDao.getARptLssuedAreasInfoById(areaId);
	}

	//保存区域和报表之间的关系
	public void saveRptLssuedAreasTemplateRelList(List<String> templateIdsList, String areaId) {
		List<RptLssuedAreasTemplateRel> rptLssuedAreasTemplateRelList = new ArrayList<RptLssuedAreasTemplateRel>();
		for (String templateId : templateIdsList) {
			RptLssuedAreasTemplateRel rptLssuedAreasTemplateRel = new RptLssuedAreasTemplateRel();
			rptLssuedAreasTemplateRel.setTemplateId(templateId);
			rptLssuedAreasTemplateRel.setAreaId(areaId);
			rptLssuedAreasTemplateRelList.add(rptLssuedAreasTemplateRel);
		}
		rptLssuedAreasInfoDao.saveRptLssuedAreasTemplateRelList(rptLssuedAreasTemplateRelList, areaId);
	}

	public List<ViewRptGroupTemplate> getAllReportByAreaIdAndAnnualId(String areaId, String annualId,
			String... templateIds) {
		List<RptLssuedAreasTemplateRel> rptLssuedAreasTemplateRelList = rptLssuedAreasInfoDao
				.getAllTemplateIdByAreaId(areaId);
		List<ViewRptGroupTemplate> viewRptGroupTemplateList = reportTemplateManageService
				.getRptTemplateByIdsLussed(templateIds);
		for (RptLssuedAreasTemplateRel rptLssuedAreasTemplateRel : rptLssuedAreasTemplateRelList) {
			for (ViewRptGroupTemplate template : viewRptGroupTemplateList) {
				if (rptLssuedAreasTemplateRel.getTemplateId().equals(template.getTemplateId())) {
					template.setChecked(true);
				}
			}
		}
		return viewRptGroupTemplateList;
	}

	public List<ViewRptGroupTemplate> getAllReportByAreaIdAndAnnualId(String areaId, String annualId) {
		List<RptLssuedAreasTemplateRel> rptLssuedAreasTemplateRelList = rptLssuedAreasInfoDao
				.getAllTemplateIdByAreaId(areaId);
		List<ViewRptGroupTemplate> viewRptGroupTemplateList = rptDealWithService.getTemplateByAnnual(annualId);
		for (RptLssuedAreasTemplateRel rptLssuedAreasTemplateRel : rptLssuedAreasTemplateRelList) {
			for (ViewRptGroupTemplate template : viewRptGroupTemplateList) {
				if (rptLssuedAreasTemplateRel.getTemplateId().equals(template.getTemplateId())) {
					template.setChecked(true);
				}
			}
		}
		return viewRptGroupTemplateList;
	}

	public List<String> getTemplatesByAreaId(String areaId) {
		List<RptLssuedAreasTemplateRel> rptLssuedAreasTemplateRelList = rptLssuedAreasInfoDao
				.getAllTemplateIdByAreaId(areaId);
		List<String> templateIds = new ArrayList<String>();
		for (RptLssuedAreasTemplateRel rptLssuedAreasTemplateRel : rptLssuedAreasTemplateRelList) {
			templateIds.add(rptLssuedAreasTemplateRel.getTemplateId());
		}
		return templateIds;
	}
}
